use std::fs;

use log::warn;

const SERIAL_NUM_FILE: &str = "/sys/firmware/devicetree/base/serial-number";
const UNKN_SERIAL_NUM: &str = "0000000000000000";

pub(crate) fn get_serial_numb() -> String {
    let mut contents = fs::read_to_string(SERIAL_NUM_FILE)
        .unwrap_or_else(|_| UNKN_SERIAL_NUM.to_string())
        .trim()
        .to_string();
    contents = contents.trim_matches(char::from(0)).to_string();
    if contents.len() != 16 {
        warn!("Wrong serial number: {} {}", contents, contents.len());
        contents = UNKN_SERIAL_NUM.to_string();
    }
    format!("bs-{}", contents)
}
